package cn.com.dcsgo.controller;

import cn.com.dcsgo.domain.dto.CustomerModifyDTO;
import cn.com.dcsgo.domain.dto.CustomerPageDTO;
import cn.com.dcsgo.domain.dto.CustomerSaveDTO;
import cn.com.dcsgo.domain.po.Customer;
import cn.com.dcsgo.domain.result.Result;
import cn.com.dcsgo.service.CustomerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

/**
 * CustomerController
 *
 * @author Dcsgo
 * @since 14/5/2025 上午 9:10
 */
@CrossOrigin
@RestController
@RequestMapping("customer")
@RequiredArgsConstructor
public class CustomerController {
    private final CustomerService customerService;

    @PostMapping
    public Result<Serializable> save(@RequestBody CustomerSaveDTO customerSaveDTO) {
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerSaveDTO, customer);
        customerService.save(customer);
        return Result.ok();
    }

    @DeleteMapping("{id}")
    public Result<Serializable> remove(@PathVariable Long id) {
        customerService.removeById(id);
        return Result.ok();
    }

    @PutMapping("{id}")
    public Result<Serializable> modify(@PathVariable Long id,
                                       @RequestBody CustomerModifyDTO customerModifyDTO) {
        Customer customer = new Customer();
        customer.setId(id);
        BeanUtils.copyProperties(customerModifyDTO, customer);
        customerService.updateById(customer);
        return Result.ok();
    }

    @GetMapping("page/{page}/{size}")
    public Result<IPage<Customer>> page(
            @PathVariable Integer page,
            @PathVariable Integer size,
            CustomerPageDTO customerPageDTO) {
        Page<Customer> pageParam = new Page<>(page, size);
        IPage<Customer> result = customerService.getPage(pageParam, customerPageDTO);
        return Result.ok(result);
    }
}
